import { TodoAction } from "../actions/todos"

type TodoList = {
  id: number
  name: string
  done: boolean
}[]

const initValue = [
  {
    id: 1,
    name: '吃饭',
    done: false,
  },
  {
    id: 2,
    name: '睡觉',
    done: true,
  },
  {
    id: 3,
    name: '打豆豆',
    done: false,
  },
]

export default function todos(state = initValue, action: TodoAction): TodoList {
  switch (action.type) {
    case "DEL_TODO":
      const list = state.filter(item => item.id !== action.id)
      return list
    case "ADD_TODO":
      return [
        {
          id: Date.now(),
          name: action.name,
          done: false
        },
        ...state
      ]
    default:
      return state
  }
}